<?php
$objXajax->registerFunction("cusfrmreportproduct_load");  

function cusfrmreportproduct_load($dateFrom,$dateTo,$reportType){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    if($reportType!=2){
      $reportType=1;  
    }
    $arrSum= array();
    $arrData = cusfrmreportproduct_createArray($dateFrom,$dateTo,$reportType,$arrSum);
    $oTbs = new clsTinyButStrong();
    
    
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/cus_reportproduct_grid'.$reportType.'.html');
    $oTbs->MergeBlock('blk_data', $arrData); 
    $oTbs->MergeBlock('blk_data_sum', $arrSum); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('cusfrmreportproduct_divGrid', 'innerHTML', $oTbs->Source);
    //$objResponse->addScriptCall("dataTableObj.CreatDataTable('cusfrmreportproduct_tbl');");  
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('cusfrmreportproduct_tbl',true);");  
    $objResponse->addScript('changeWaiting(1);');                     
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function cusfrmreportproduct_createArray($dateFrom,$dateTo,$reportType,&$arrSum){
    global $objDbUpdate,$tbl_postfix;
    $sqlGroup='';
    $maxIdProduct = 100000000;
    if($reportType==2){
            $sql = "select name, price,if(price<0,1,0) protype from  cus_history_product$tbl_postfix  where
            datediff(date_created,'$dateFrom')>=0 and datediff(date_created,'$dateTo')<=0   and product_id > $maxIdProduct
              order by  name asc ";
        
    }else{
            $sql = "select sum(num) as num , name,sum(num*(price+area_extprice)) price from  cus_history_product$tbl_postfix  where
            datediff(date_created,'$dateFrom')>=0 and datediff(date_created,'$dateTo')<=0 and product_id < $maxIdProduct
             group by product_id order by  name asc ";
    }

    $arr = array();
    //echo $sql;
    try{
        $arr = $objDbUpdate->GetArray($sql);
        $num =0;
        $price = 0;
        $count = count($arr);
        if($reportType==1){
            for($i=0;$i<$count;$i++){
                $num += $arr[$i]['num'];  
                $price += $arr[$i]['price'];  
            }
            
        }else{
            for($i=0;$i<$count;$i++){
                $price += $arr[$i]['price']; 
                if($arr[$i]['price']<0){
                    $num++;
                } 
            }
            
        }
        
        $arrSum[0]['num']= $num; 
        $arrSum[0]['price']= $price; 
    }
    catch(Exception $e){
        $arr = array();
    } 
    return  $arr  ;
} 


# end region process data
?>
